package com.hrm.service.impl;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hrm.mapper.UsersMapper;
import com.hrm.pojo.Users;
import com.hrm.service.UsersService;
import com.hrm.utils.DuanxinUtils;
import com.hrm.utils.entity.PageResult;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.alibaba.druid.support.json.JSONUtils.toJSONString;

@Service
@Transactional
public class UsersServiceImpl implements UsersService {
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private DuanxinUtils duanxinUtils;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;//密码加密

    @Override
    public boolean register(Users users) throws Exception {
        //查询用户是否存在
        Users users1 = usersMapper.selectByLoginname(users.getLoginname());
        if (users1 != null) {
            throw new Exception("登录名已被使用，请重新添加");
        }
        //查询号码是否存在
        Users users2 = usersMapper.selectByPhone(users.getPhone());
        if (users2 != null) {
            throw new Exception("该手机号已被注册，请更换手机号");
        }
        //将用户的密码进行加密
        String newPwd = bCryptPasswordEncoder.encode(users.getPassword());
        //将加密后的密码设置到用户对象中
        users.setPassword(newPwd);
        //补充创建时间的字段
        users.setCreatedate(new Date());
        if (usersMapper.insertSelective(users) > 0)
            return true;
        return false;
    }

    @Override
    public Users login(String LonginName, String pwd) throws Exception {
        /**
         * 1.根据登录用户名查找用户信息
         * 2.根据查找到的用户的密码和输入的密码进行校验
         * 3.校验通过返回true，校验失败返回false
         */
        //前端不能校验
        if (LonginName == null || pwd == null) {
            throw new Exception("用户名或密码不能为空");
        }
        Users users = usersMapper.selectByLoginName(LonginName);
        if (users == null) {
            throw new Exception("用户名不存在");
        }
        //校验密码  强哈希，相同的密码每一次加密都不一样
        if (!bCryptPasswordEncoder.matches(pwd, users.getPassword())) {
            throw new Exception("密码错误");
        }
        return users;
    }

    @Override
    public boolean updatePwd(Integer id, String oldPwd, String newPwd) throws Exception {
        //查询用户信息
        Users users = usersMapper.selectByPrimaryKey(id);
        if (users == null) {
            throw new Exception("用户不存在");
        }
        //校验密码
        if (!bCryptPasswordEncoder.matches(oldPwd, users.getPassword())) {
            throw new Exception("原密码错误");
        }
        //加密新密码
        String newPwdEncode = bCryptPasswordEncoder.encode(newPwd);
        //更新密码
        users.setId(id);
        users.setPassword(newPwdEncode);
        if (usersMapper.updatePasswordByPrimaryKey(users) > 0)
            return true;
        return false;
    }

    @Override
    public PageResult<Users> findByUsernameAndStatus(Integer pageNum, Integer pageSize, Users users) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        List<Users> result = usersMapper.selectByUsernameAndStatus(users.getUsername(), users.getStatus());
        if (result == null || result.size() == 0) {
            throw new Exception("没有这条数据");
        }
        PageInfo<Users> pageInfo = new PageInfo<>(result);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public boolean deleteUsers(Integer[] ids) throws Exception {
        //根据id查询用户是否存在
        for (Integer id : ids) {
            Users users = usersMapper.selectByPrimaryKey(id);
            if (users == null) {
                throw new Exception(users.getUsername() + "用户不存在");
            }
        }
        //删除用户
        int i = usersMapper.deleteByPrimaryKeys(ids);
        return i > 0 ? true : false;
    }

    @Override
    public Users findById(Integer id) throws Exception {
        Users users = usersMapper.selectByPrimaryKey(id);
        if (users == null) {
            throw new Exception("用户不存在");
        }
        return users;
    }

    @Override
    public boolean updateUser(Users users) throws Exception {
        //根据id查询用户存不存在，排除特殊情况管理员刚好删除
        Users users1 = usersMapper.selectByPrimaryKey(users.getId());
        if (users1 == null) {
            throw new Exception("用户不存在");
        }
        //查询传入的登录号是否存在
        Users users2 = usersMapper.selectByLoginName(users.getLoginname());
        if (!users.getLoginname().equals ( users1.getLoginname()) && users2!= null){
            throw new Exception("登录号已被使用，请输入新的登录号");
        }
        //查询传入的手机号是否存在
        Users users3 = usersMapper.selectByPhone(users.getPhone());
        if (!Objects.equals(users.getPhone(), users1.getPhone()) && users3!=null){
            throw new Exception("该手机号已被注册，请输入新的手机号");
        }
        int i = usersMapper.updateByPrimaryKeys(users);
        return i > 0 ? true : false;
    }

    @Override
    public Users findByPhone(String phone) throws Exception {
        Users users = usersMapper.selectByPhone(phone);
        if (users == null) {
            throw new Exception("该手机号未注册");
        }
        return users;
    }


    @Override
    public boolean updatePwdByPhone(String phone, String newPwd) throws Exception {
        //根据手机号查询用户是否存在
        Users users = usersMapper.selectByPhone(phone);
        if (users == null)
            throw new Exception("该手机号未注册");
        //加密新密码
        String newPwdEncode = bCryptPasswordEncoder.encode(newPwd);
        int i = usersMapper.updatePasswordByPhone(phone, newPwdEncode);
        return i > 0 ? true : false;
    }

}
